package cn.rtplay.esp32camcar;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.rtplay.esp32camcar.databinding.ItemBinding;

public class ScanRecyclerAdapter extends RecyclerView.Adapter<ScanRecyclerAdapter.ViewHolder> {
    //数据源
    private final List<ScanItem> mList;

    public ScanRecyclerAdapter(List<ScanItem> list) {
        mList = list;
    }

    //返回item个数
    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addData(int position, ScanItem item) {
        mList.add(position, item);
        notifyItemInserted(position);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addItem(List<ScanItem> list) {
        list.addAll(mList);
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clearAllItem() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void addDataAppend(ScanItem item) {
        mList.add(item);
        notifyItemInserted(getItemCount());
    }

    public boolean findItemBySSID(String ssid) {
        for (ScanItem item: mList
             ) {
            if (item.getWifiSSID().equals(ssid)) {
                return true;
            }
        }
        return false;
    }

    //创建ViewHolder
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemBinding viewBinding = ItemBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new ViewHolder(viewBinding);
    }

    //定义接口 OnItemClickListener
    public interface OnItemClickListener {
        void onItemClick(ViewHolder view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    //填充视图
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        holder.carNO.setText(mList.get(position).getCarNO());
        holder.carSSID.setText(mList.get(position).getWifiSSID());
        holder.carModel.setText(mList.get(position).getCarModel());
        holder.carBrand.setImageResource(mList.get(position).getCarBrandId());
        holder.carModel.setTextColor(Color.parseColor(mList.get(position).getCarModelColor()));
        if (mOnItemClickListener != null) {
            holder.carNO.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder, pos);
            });
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ItemBinding viewBinding;
        AppCompatTextView carNO;
        AppCompatImageView carBrand;
        AppCompatTextView carModel;
        AppCompatTextView carSSID;

        public ViewHolder(ItemBinding itemView) {
            super(itemView.getRoot());
            viewBinding = itemView;
            carNO = viewBinding.carNO;
            carBrand = viewBinding.carBrand;
            carModel = viewBinding.carModel;
            carSSID = viewBinding.ssidAddress;
        }
    }
}
